package net.bwie.realtime.jtp.dws.log.job;

import com.alibaba.fastjson.JSON;
import net.bwie.realtime.jtp.dws.log.bean.PageViewBean;
import net.bwie.realtime.jtp.dws.log.function.PageViewBeanMapFunction;
import net.bwie.realtime.jtp.dws.log.function.PageViewWindowFunction;
import net.bwie.realtime.jtp.utils.DorisUtil;
import net.bwie.realtime.jtp.utils.KafkaUtil;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.datastream.WindowedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;

import java.time.Duration;

/**
 * @BelongsProject: realtime-project-10zlq
 * @BelongsPackage: net.bwie.realtime.jtp.dws.log.job
 * @Author: zhangleqing
 * @CreateTime: 2025-08-18  11:37
 * @Description: TODO
 * @Version: 1.0
 */
public class JtpTrafficPageViewMinuteWindowDwsJob {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        // 配置检查点3秒一次
        env.enableCheckpointing(3000L);

        // 数据读取
        DataStream<String> pageStream = KafkaUtil.consumerKafka(env, "dwd-traffic-page-log");

        // 数据处理
        DataStream<String> resultStream = handle(pageStream);

        // 数据写入Doris+展示
        DorisUtil.saveToDoris(resultStream, "jtp_realtime_report", "dws_traffic_page_view_window_report");
        resultStream.print("展示==>");

        env.execute("JtpTrafficPage");
    }

    private static DataStream<String> handle(DataStream<String> stream) {
        // 第一种写法
        KeyedStream<String, String> midStream = stream.keyBy(
                json -> JSON.parseObject(json).getJSONObject("common").getString("mid")
        );

        // 第二种写法
//        KeyedStream<String, String> midStream = stream.keyBy(new KeySelector<String, String>() {
//            @Override
//            public String getKey(String s) throws Exception {
//                return JSON.parseObject(s).getJSONObject("common").getString("mid");
//            }
//        });

        // s2-讲流中每条日志数据封装实体类Bean对象
        DataStream<PageViewBean> beanStream = midStream.map(new PageViewBeanMapFunction());

        // s3-事件时间字段和水位线
        DataStream<PageViewBean> timeStream = beanStream.assignTimestampsAndWatermarks(
                WatermarkStrategy
                        .<PageViewBean>forBoundedOutOfOrderness(Duration.ofSeconds(0))
                        .withTimestampAssigner(
                                new SerializableTimestampAssigner<PageViewBean>() {
                                    @Override
                                    public long extractTimestamp(PageViewBean pageViewBean, long l) {
                                        return pageViewBean.getTs();
                                    }
                                }
                        )
        );

        // s4-分组keyBy：ar地区，ba品牌，ch渠道，is_new新老访客
        KeyedStream<PageViewBean, String> keyedStream = timeStream.keyBy(
           bean -> bean.getBrand() + "," + bean.getChannel() + "," + bean.getProvince() + "," + bean.getIsNew()
        );

        // s5-开窗：滚动窗口，滚动窗口大小为1分钟
        WindowedStream<PageViewBean, String, TimeWindow> windowStream = keyedStream.window(TumblingEventTimeWindows.of(Time.minutes(1))
        );

        // s6-聚合：对窗口中数据计算
        DataStream<String> reportStream = windowStream.apply(new PageViewWindowFunction());

        // 返回结果
        return reportStream;
    }
}
